
// 测试环境
let baseUrlsrongyun = 'https://test-play.zbwpay.com/unmannedapi/mini'//融云
let baseUrls =  'https://buff.jirentech.com/api'
// let baseUrls = 'http://47.119.116.90:8001'


import store from '../store'
	
interface Response {
	code : number
	data : object
	message : string
}

type methods = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'

export const http = (method : methods, url : string, data ?: object) => {//请求封装
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrls + url, //如果是get请求，把获取到的值拼在后面，否则不用
			method,
			data, //如果是post请求，就把data赋值，否则就空
			header: {
				'Content-Type': 'application/json',
				Authorization:'Bearer '+uni.getStorageSync('app_token')
			},
			success: (res : any) => {
				const data = res.data as Response
				console.log('urlurl',baseUrls + url)
				console.log('datadata',data)
				if (res.statusCode == 200) {
					if (data.code == 200) {
						resolve(data)
					}else{
						reject(data)
						uni.showToast({
							title: data.message,
							icon: 'none'
						
						})
						
					}

				} else if(res.statusCode == 401||res.statusCode == 403){
					uni.setStorageSync('app_token', null)
					uni.setStorageSync('rongyun_token',null)
					uni.reLaunch({
						url:'/pages/login/index'
					})
					reject(data)
				}else {
					reject(data)
					uni.showToast({
						title: data?.message || '网络异常',
						icon: 'none'
					})
				}
			}, fail: (err : any) => {
				console.log('urlurl',baseUrls + url)
				console.log('errerrerr',err)
				reject(err)
				uni.showToast({
					title: "网络异常，请稍后重试！",
					icon: 'none',
					duration: 2000,
				})
			}
		})
	})

}


export const uploadFile = (filePath:any,formData:any) => {//上传封装
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url:  baseUrls + "/file/upload", // post请求地址
			filePath,
			formData,
			name: 'upload_file',  
			header: {
				'Content-Type': 'application/json',
				Authorization:'Bearer '+uni.getStorageSync('app_token')
			},
			success: function (uploadFileRes) {
				const res = JSON.parse(uploadFileRes.data)
				console.log('上传返回',res);
				resolve(res)
			},
			fail: function (fail) {
				uni.showToast({
					title: '上传失败',
					icon: 'none',
					duration: 2000,
				})
			}
		});
	})
}

export default {
	baseUrls
}